home *** CD-ROM | disk | FTP | other *** search
- {****************************************************}
- {}
- { CStarterDoc.p }
- {}
- { Document methods for a typical application. }
- {}
- { Copyright ⌐ 1989, Symantec Corporation. All rights reserved. }
- {}
- {****************************************************}
-
-
- unit CStarterDoc;
-
- interface
-
- uses
- TCL, StarterIntf;
-
- implementation
-
-
- {**}
- { * IStarterDoc}
- { *}
- { * This is your document's initialization method.}
- { * If your document has its own instance variables, initialize}
- { * them here.}
- { *}
- { * The least you need to do is initialize the superclass.}
- { *}
- { **}
-
- procedure CStarterDoc.IStarterDoc (aSupervisor: CApplication; printable: Boolean);
-
- begin
- IDocument(aSupervisor, printable);
- end;
-
-
- {**}
- { * Free}
- { *}
- { * This is your document's destruction method.}
- { * If you allocated memory in your initialization method}
- { * or opened temporary files, this is the place to release them.}
- { *}
- { * Be sure to call the inherited method!}
- { *}
- { **}
-
- procedure CStarterDoc.Free;
-
- begin
- inherited Free;
- end;
-
-
- {**}
- { * DoCommand}
- { *}
- { * In this method, you handle all the commands your document}
- { * deals with.}
- { *}
- { * Be sure to call the inherited method to handle the standard}
- { * document commands: cmdClose, cmdSave, cmdSaveAs, cmdRevert,}
- { * cmdPageSetup, cmdPrint, and cmdUndo. To change the way these}
- { * commands are handled, override the appropriate methods instead}
- { * of handling them here.}
- { *}
- { **}
-
- procedure CStarterDoc.DoCommand (theCommand: longint);
-
- begin
- case theCommand of
-
- { Your document commands go here (dummyCmd is given as an example) }
-
- dummyCmd:
- begin
- { code for dummyCmd }
- end;
-
- otherwise
- inherited DoCommand(theCommand);
- end;
- end;
-
- {**}
- { * UpdateMenus}
- { *}
- { * In this method you can enable menu commands that apply when}
- { * your document is active.}
- { *}
- { * Be sure to call the inherited method to get the default behavior.}
- { * The inherited method enables these commands: cmdClose, cmdSaveAs,}
- { * cmdSave, cmdRevert, cmdPageSetup, cmdPrint, cmdUndo.}
- { *}
- { **}
-
- procedure CStarterDoc.UpdateMenus;
-
- begin
- inherited UpdateMenus;
-
- { Enable your menu commands here (enable each one with a call to }
- { gBartender.EnableCmd(command_number)). }
-
- end;
-
-
- {**}
- { * NewFile}
- { *}
- { * When the user chooses New from the File menu, the CreateDocument}
- { * method in your Application class will send a newly created document}
- { * this message. This method needs to create a new window, ready to}
- { * work on a new document.}
- { *}
- { * Since this method and the OpenFile method share the code for creating}
- { * the window, you should use an auxiliary window-building method.}
- { *}
- { **}
-
- procedure CStarterDoc.NewFile;
- var
- wTitle: Str255; { Window title string }
- wCount: integer; { Index number of new window }
- wNumber: Str255; { Index number as a string }
-
- begin
- {*}
- { ** BuildWindow is the method that}
- { ** does the work of creating a window.}
- { ** Its parameter should be a handle to the data that}
- { ** you want to display in the window.}
- { ** Since this is a new window, there's nothing}
- { ** to display.}
- { **}
- { *}
-
- BuildWindow(nil);
-
- {*}
- { ** Append an index number to the}
- { ** default name of the window.}
- { *}
-
- itsWindow.GetTitle(wTitle);
- wCount := gDecorator.GetWCount;
- NumToString(wCount, wNumber);
- wTitle := concat(wTitle, ' ');
- wTitle := concat(wTitle, wNumber);
- itsWindow.SetTitle(wTitle);
-
- {*}
- { ** Send the window a Select message to make}
- { ** it the active window.}
- { *}
-
- itsWindow.Select;
- end;
-
-
- {**}
- { * OpenFile}
- { *}
- { * When the user chooses Open╔ from the File menu, the OpenDocument}
- { * method in your Application class will let the user choose a file}
- { * and then send a newly created document this message. The information}
- { * about the file is in the SFReply record.}
- { *}
- { * In this method, you need to open the file and display its contents}
- { * in a window. This method uses the auxiliary window-building method.}
- { *}
- { **}
-
- procedure CStarterDoc.OpenFile (macSFReply: SFReply);
-
- var
- theFile: CDataFile;
- theData: Handle;
- theName: Str255;
- theError, temp: OSErr;
- tempBool: Boolean;
-
- begin
- {*}
- { ** Create a file and send it an SFSpecify}
- { ** message to set up the name, volume, and}
- { ** directory.}
- { **}
- { *}
-
- new(theFile);
- theFile.IDataFile;
- theFile.SFSpecify(macSFReply);
-
- {*}
- { ** Be sure to set the instance variable}
- { ** so other methods can use the file if they}
- { ** need to. This is especially important if}
- { ** you leave the file open in this method.}
- { ** If you close the file after reading it, you}
- { ** should be sure to set itsFile to nil.}
- { **}
- { *}
-
- itsFile := theFile;
-
- {*}
- { ** Send the file an Open message to}
- { ** open it. You can use the ReadSome or}
- { ** ReadAll methods to get the contents of the file.}
- { **}
- { *}
-
- theError := theFile.Open(fsRdWrPerm);
-
- {*}
- { ** Check to see if we were able to open}
- { ** the file. Send the error handler}
- { ** a CheckOSError message. If there was}
- { ** an error, CheckOSError returns false}
- { ** and reports the error in an alert.}
- { ** The default error message displays the}
- { ** error number.}
- { ** You can use Estr resources to customize}
- { ** the error message.}
- { **}
- { ** Note that we send ourselves a Free}
- { ** message. Since we're not going to open,}
- { ** we should get rid of the object.}
- { *}
-
- if not gError.CheckOSError(theError) then
- begin
- Free;
- Exit(OpenFile);
- end;
-
- {*}
- { ** Make sure that the memory request to read}
- { ** the data from the file doesn't use up any}
- { ** of our rainy day fund and that the GrowMemory}
- { ** method (in the application) knows that it's OK}
- { ** if we couldn't get enough memory.}
- { **}
- { *}
-
- gApplication.RequestMemory(FALSE, TRUE);
- temp := theFile.ReadAll(theData); { ReadAll creates the handle }
- gApplication.RequestMemory(FALSE, FALSE); { Reset canFail to FALSE for }
- { default memory-error handling }
- {}
- {}
- { {*}
- { ** If there isn't enough memory to open,}
- { ** post the error (should be -108, memFullErr)}
- { ** and get rid of ourselves.}
- { **}
- { *}
-
- if theData = nil then
- begin
- tempBool := gError.CheckOSError(MemError);
- Free;
- Exit(OpenFile);
- end;
-
-
- BuildWindow(theData);
-
- {*}
- { ** In your application, you'll probably store}
- { ** the data in some form as an instance variable}
- { ** in your document class. For this example, there's}
- { ** no need to save it, so we'll get rid of it.}
- { **}
- { *}
-
- DisposHandle(theData);
-
- {*}
- { ** In this implementation, we leave the file}
- { ** open. You might want to close it after}
- { ** you've read in all the data.}
- { **}
- { *}
-
- itsFile.GetName(theName);
- itsWindow.SetTitle(theName);
- itsWindow.Select; { Don't forget to make the window active }
- end;
-
-
-
- {**}
- { * BuildWindow}
- { *}
- { * This is the auxiliary window-building method that the}
- { * NewFile and OpenFile methods use to create a window.}
- { *}
- { * In this implementation, the argument is a handle to }
- { * the data to display.}
- { *}
- { **}
-
- procedure CStarterDoc.BuildWindow (theData: Handle);
-
- var
- theScrollPane: CScrollPane;
- theMainPane: CStarterPane;
- theWindow: CWindow; { Altered by TCL Weaver 1.0 (5/9/90) }
-
- begin
- {*}
- { ** First create the window and initialize}
- { ** it. The first argument to IWindow is the resource ID}
- { ** of the window. The second argument specifies}
- { ** whether the window is a floating window.}
- { ** The third argument is the window's enclosure; it}
- { ** should always be gDesktop. The last argument is}
- { ** the window's supervisor in the Chain of Command;}
- { ** it should always be the Document object.}
- { **}
- { *}
-
- new(theWindow); { Altered by TCL Weaver 1.0 (5/9/90) }
- itsWindow := theWindow;
- itsWindow.IWindow(WINDStarter, FALSE, gDesktop, SELF);
-
- {*}
- { ** After you create the window, you can use the}
- { ** SetSizeRect message to set the window╒s maximum}
- { ** and minimum size. Be sure to set the max & min}
- { ** BEFORE you send a PlaceNewWindow message to the}
- { ** decorator.}
- { **}
- { ** The default minimum is 100 by 100 pixels. The}
- { ** default maximum is the bounds of GrayRgn (the}
- { ** entire display area on all screens.)}
- { **}
- { ** We'll use the defaults.}
- { **}
- { *}
-
- { *}
- { ** Our window will contain a ScrollPane, }
- { ** which in turn will contain a Panorama.}
- { ** Now, let's create the ScrollPane.}
- { *}
-
-
- new(theScrollPane);
-
- {*}
- { ** There are two ways to initialize a scroll pane:}
- { ** 1. You can specify all the values}
- { ** right in your code, as we do in this example.}
- { ** 2. You can create a ScPn resource and}
- { ** initialize the pane from the information}
- { ** in the resource.}
- { **}
- { *}
-
- theScrollPane.IScrollPane(itsWindow, SELF, 10, 10, 0, 0, sizELASTIC, sizELASTIC, TRUE, TRUE, TRUE);
-
- {*}
- { ** The FitToEnclFrame method makes the}
- { ** scroll pane be as large as its enclosure.}
- { ** In this case, the enclosure is the window,}
- { ** so the scroll pane will take up the entire}
- { ** window.}
- { **}
- { *}
-
- theScrollPane.FitToEnclFrame(TRUE, TRUE);
-
-
- {*}
- { ** itsMainPane is the document's focus}
- { ** of attention. Some of the standard}
- { ** classes (particularly CPrinter) rely}
- { ** on itsMainPane pointing to the main}
- { ** pane of your window.}
- { **}
- { ** itsGopher specifies which object}
- { ** should become the gopher when}
- { ** the document becomes active. By default,}
- { ** the document becomes the gopher. It╒s}
- { ** likely that your main pane handles commands}
- { ** so you╒ll almost always want to set itsGopher}
- { ** to point to the same object as itsMainPane.}
- { **}
- { ** Note that the main pane is the}
- { ** panorama in the scroll pane and not}
- { ** the scroll pane itself.}
- { **}
- { *}
-
- new(theMainPane);
- theMainPane.IStarterPane(theScrollPane, SELF, 0, 0, 0, 0, sizELASTIC, sizELASTIC);
- itsMainPane := theMainPane;
- itsGopher := theMainPane;
-
- {* The FitToEnclosure method makes the pane}
- { ** fit inside the enclosure. The inside (or}
- { ** interior) of a scroll pane is defined as}
- { ** the area inside the scroll bars.}
- { *}
-
- theMainPane.FitToEnclosure(TRUE, TRUE);
-
- {*}
- { ** Send the scroll pane an InstallPanorama message}
- { ** to associate the panorama with the scroll pane.}
- { **}
- { *}
-
- theScrollPane.InstallPanorama(theMainPane);
-
- {*}
- { ** The Decorator is a global object that takes care}
- { ** of placing and sizing windows on the screen.}
- { ** You don't have to use it.}
- { **}
- { *}
-
- gDecorator.PlaceNewWindow(itsWindow);
- end;
-
-
- {**}
- { * DoSave}
- { *}
- { * This method handles what happens when the user chooses Save from the}
- { * File menu. This method should return TRUE if the file save was successful.}
- { * If there is no file associated with the document, you should send a}
- { * DoSaveFileAs message.}
- { *}
- { **}
-
- function CStarterDoc.DoSave: Boolean;
-
- begin
- {*}
- { ** If you closed your file in your NewFile method,}
- { ** you'll need a different way than this to determine}
- { ** if there's a file associated with your document.}
- { **}
- { *}
-
- if itsFile = nil then
- DoSave := DoSaveFileAs
- else
- begin
-
- {*}
- { ** In your application, this is where you'd}
- { ** write out your file. If you left it open,}
- { ** send the WriteSome or WriteAll message}
- { ** to itsFile.}
- { **}
- { *}
-
- dirty := FALSE; { Document is no longer dirty }
- gBartender.DisableCmd(cmdSave);
- DoSave := TRUE; { Save was successful }
- end;
- end;
-
-
- {**}
- { * DoSaveAs}
- { *}
- { * This method handles what happens when the user chooses Save As╔ from the}
- { * File menu. The default DoCommand method for documents sends a DoSaveFileAs}
- { * message which displays a standard put file dialog and sends this message.}
- { * The SFReply record contains all the information about the file you're about}
- { * to create.}
- { *}
- { **}
-
- function CStarterDoc.DoSaveAs (macSFReply: SFReply): Boolean;
- var
- temp: OSErr;
- theFile: CFile; { Altered by TCL Weaver 1.0 (5/9/90) }
-
- begin
- {*}
- { ** If there's a file associated with this document}
- { ** already, close it. The Free method for files}
- { ** sends a Close message to the file before releasing}
- { ** its memory.}
- { **}
- { *}
-
- if itsFile <> nil then
- itsFile.Free;
-
-
- {*}
- { ** Create a new file, and then save it normally.}
- { **}
- { *}
-
- new(CDataFile(theFile)); { Altered by TCL Weaver 1.0 (5/9/90) }
- itsFile := theFile;
- CDataFile(itsFile).IDataFile;
- itsFile.SFSpecify(macSFReply);
- temp := itsFile.CreateNew(gSignature, 'TEXT');
- temp := itsFile.Open(fsRdWrPerm);
-
- itsWindow.SetTitle(macSFReply.fName);
-
- DoSaveAs := DoSave;
- end;
-
-
- {**}
- { * DoRevert}
- { *}
- { * If your application supports the Revert command, this method}
- { * should close the current file (without writing anything out)}
- { * and read the last saved version of the file.}
- { *}
- { **}
-
- procedure CStarterDoc.DoRevert;
-
- begin
- end;
-
-
- end.